 
<?php $user = $this->user->get();
$condition ="";

if($this->user->is_role(3))
{

	$condition = "and  allocations.user_id =".$user["user_id"];
}


$sql  ="SELECT users.firstname, 
	users.lastname, 
	allocations.allocation_id, 
	lpad(allocations.allocation_id, 5,'0') AS `code`, 
	date_format(allocations.allocation_date,'%d/%m/%Y') AS allocation_date, 
	allocations.user_id, 
	allocations.detail, 
	allocations.return_date, 
	allocations.allocation_type, 
	place.place_name, 
	allocations.place_id
FROM allocations LEFT OUTER JOIN users ON allocations.user_id = users.user_id
	 left JOIN place ON allocations.place_id = place.place_id
WHERE 1 and allocation_id=$id $condition";

$rows = $this->db->query($sql)->result_array();
$row = $rows[0];
//print_r($rows);
?>
<table class="list">
<tr>
<th style="width: 120px;" >เลขที่ขอเบิกจ่าย</th>
<th><?php echo $row['code'];  ?></th>
</tr>
<tr>
<td>วันที่ขอเบิกจ่าย</td>
<td><?php echo $row['allocation_date'];  ?></td>
</tr>
<tr>
<td>ชื่อ-นามสกุล</td>
<td><?php echo $row['firstname']." ".$row["lastname"];  ?></td>
</tr>

<tr>
	<td>รายละเอียด/หมายเหตุ</td>
	<td><?php echo $row['detail'];  ?></td>
</tr>
<tr>
	<td>ห้องปฎิบัติการ/สถานที่</td>
	<td><?php echo $row['place_name'];  ?></td>
</tr>
</table>

รายการขอเบิกจ่าย
<table class="list">
	<thead>
	
	       
	         <th style="width:   120px;">หมวดวัสดุ-ครุภัณฑ์</th>
	         
	          <th style="width: 60px;">จำนวนขอเบิกจ่าย</th>
	           <th style="width: 150px;">รายละเอียด/หมาเหตุ</th>
	          
	         <th style="width: 400px;">วัสดุ-ครุภัณฑ์ที่อนุมัติ</th>
	        
	      
	</thead>
	<tbody>
	<?php $sql = "SELECT allocation_items.allocation_id, 
		allocation_items.amount, 
		allocation_items.remark, 
		allocation_items.return_date, 
		allocation_items.`return`, 
		allocation_items.category_id, 
		category_views.category_name, 
		category_views.category_id, 
		category_views.cat_base_name, 
		category_views.sort_order,
		allocation_item_id
	FROM allocation_items left  JOIN category_views ON allocation_items.category_id = category_views.category_id where allocation_id=".$row["allocation_id"];
	
	$items = $this->db->query($sql)->result_array();
	
	 foreach($items as $item)
	 {
	 
	 
	
	 ?>
	 
	 <tr>
	 <td><?php echo $item['category_name']; ?></td>
	 <td><?php echo $item['amount']; ?></td>
	 <td><?php echo $item['remark']; ?></td>
	 <td>
	 
	 
	  
	
	
	  
	 <?php $sub_items =  $this->db->query("SELECT sub_alloc_item_id, sub_allocation_items.`code`, 
	sub_allocation_items.amount, 
	material_registers.`name`
FROM sub_allocation_items INNER JOIN material_items ON sub_allocation_items.`code` = material_items.`code`
	 INNER JOIN material_registers ON material_items.material_id = material_registers.material_id where allocation_item_id =".$item["allocation_item_id"])->result_array();
  if(count($sub_items)){
	 ?>
	  <table class="list inner">
	  <thead>
	  <th>วัสดุครุภัณฑ์</th>
	  <th>จำนวน</th>
	  <?php if($this->user->is_role(1))
{ ?>   <th>ยกเลิก</th> <?php  } ?>
	  </thead>
	   <tbody>
	 <?php 
	 foreach($sub_items as  $sub_item){
	 
	  ?>
	 
	 <tr>
	 <td> <a href="<?php echo site_url("admin/fine_code/".urlencode(str_replace("/","-",$sub_item["code"]))); ?>">[<?php echo $sub_item["code"]; ?>] <?php echo $sub_item["name"]; ?></a></td>
	 <td><?php echo $sub_item["amount"];?></td>
	<?php if($this->user->is_role(1)){ ?>
	 <td><a href="javascript:void(0);" onclick="sub_item_delete('<?php echo $sub_item["sub_alloc_item_id"]; ?>');">ยกเลิก</a></td>
	 <?php } ?>
	 </tr>
	  
	 <?php } ?>
	  </tbody>
	
	</table>
	
	  
	<?php } ?>
	 <?php if($this->user->is_role(1))
	 { ?>
	 <button onclick="open_popup('<?php echo site_url('admin/approve_popup/'.$item["allocation_item_id"]); ?>');" >อนุมัติครุภัณฑ์</button>
	<?php } ?>
	 	 </td>
	 </tr>
	 <?php } ?>
	</tbody>
	
</table>
<script type="text/javascript">

function sub_item_delete(id)
{


	if(confirm("ยืนยันการยกเลิก")){
	$.ajax({
		  url: '<?php echo site_url('service/sub_allocation_item_delete');?>/'+id,
		  success: function(data) {
		    $('.result').html(data);
		    window.location.reload();
		  }
		});
	}
}

</script>